//HelloController.java
package org.example.jiuyejava.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.jiuyejava.entity.ApiResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;


@RestController
public class HelloController {


//    接口请求示例
//    http://localhost:8080/
    @GetMapping("/")
    public String index() {
        return "Hello from controller!";
    }

    @GetMapping("/testget")
    public String greet(@RequestParam String name) {
        return "Hello, " + name;
    }

    @PostMapping("/testpost")
    public String sayHelloPost(@RequestBody String name) {
        return "Hello, " + name;
    }
    //    所有就业信息
    @GetMapping("/jiuyelist")
    public ApiResponse jiuyelist() throws IOException {
        ClassPathResource resource = new ClassPathResource("jiuyelist.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }

    //    教师列表
    @GetMapping("/teachers")
    public ApiResponse<Object> teachers() throws IOException {
        // 读取本地resources下文件内容
//        ClassPathResource resource = new ClassPathResource("teachers.txt");
//        InputStream inputStream = resource.getInputStream();
//        return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

        ClassPathResource resource = new ClassPathResource("teachers.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
    //学生列表
    @GetMapping("/studens")
    public ApiResponse<Object> studens() throws IOException {
        // 读取本地resources下文件内容
        ClassPathResource resource = new ClassPathResource("studens.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
    //    就业率
    @GetMapping("/jiuyelv")
    public ApiResponse jiuyelv() throws IOException {
        ClassPathResource resource = new ClassPathResource("jiuyelv.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
    //    行业分布
    @GetMapping("/hangyefenbu")
    public ApiResponse<Object> hangyefenbu() throws IOException {
        ClassPathResource resource = new ClassPathResource("hangyefenbu.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
    //薪资范围
    @GetMapping("/xinzifanwei")
    public ApiResponse<Object> xinzifanwei() throws IOException {
        ClassPathResource resource = new ClassPathResource("xinzifanwei.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
    //    班级
    @GetMapping("/banji")
    public ApiResponse<Object> banji() throws IOException {
        ClassPathResource resource = new ClassPathResource("banji.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            ObjectMapper objectMapper = new ObjectMapper();
            // 将字符串解析为Map（或根据实际情况使用合适的类型）
            Object data = objectMapper.readValue(content, Object.class);
            return ApiResponse.success(data);
        }
    }
}
